package com.infore.airsupervise.app;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;

import com.infore.airsupervise.bean.AppInfoE;

import java.util.LinkedList;
import java.util.List;

/**
 * 应用信息类
 * <p>
 * 作者: dingxinlin
 * 创建日期: 2017/4/7 13:41
 */

public class AppInfo {

    public static String[] paks = new String[]{"com.baidu.BaiduMap",        //百度
            "com.autonavi.minimap"};     //高德

    public static float DENSITY;            // 屏幕密度（像素比例）
    public static float DENSITY_DPI;        // 屏幕密度（每寸像素）
    public static int SCREEN_WIDTH;         // 屏幕宽（像素）
    public static int SCREEN_HEIGHT;        // 屏幕高（像素）

    /**
     * 初始化
     * @param ctx
     */
    public static void init(Context ctx) {
        DisplayMetrics dm = ctx.getResources().getDisplayMetrics();

        DENSITY = dm.density;
        DENSITY_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
    }

    /**
     * 通过包名获取应用信息
     * @param context
     * @param packageName
     * @return
     */

    public static AppInfoE getAppInfoByPak(Context context , String packageName){
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> list = pm.getInstalledPackages(0);
        for(PackageInfo info : list){
            if(packageName.equals(info.packageName)){
                AppInfoE appInfoE = new AppInfoE();
                appInfoE.setAppName(info.applicationInfo.loadLabel(pm).toString());
                appInfoE.setIcon( info.applicationInfo.loadIcon(pm));
                appInfoE.setPackageName(info.packageName);
                return appInfoE ;
            }
        }
        return null;
    }
    /**
     * 返回当前设备上的地图应用集合
     * @param context
     * @return
     */
    public static List<AppInfoE> getMapApps(Context context) {
        LinkedList<AppInfoE> apps = new LinkedList<AppInfoE>();
        for (String pak : paks) {
            AppInfoE appinfo = getAppInfoByPak(context,pak);
            if (appinfo!=null) {
                apps.add(appinfo);
            }
        }
        return apps;
    }

}
